// pages/statistics/Income.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        startdate: '',//开始时间
        enddate: '',//结束时间
        selectshop: 0,//选中的店铺index
        shoplist: [],//店铺列表
        incomedata: {
            totail: 0,
            listdata: []//列表数据
        },
        openindex: -1,//展开的index值
        tp: 1,//统计类型,1收入统计,2业绩统计
        iscustom: false,//是否是自定义选择时间
        dateindex: 1,
        param: {
            reg: "",
            startDate: "",
            endDate: "",
            isIncome: false
        },//请求列表的参数
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        var _this = this;
        var time = app.common.getTimeSection(1, "yyyy-MM-dd");
        if (options.tp) {
            _this.setData({
                "tp": parseInt(options.tp),
                "param": {
                    "startDate": time.sdate,
                    "endDate": time.edate,
                    "isIncome": parseInt(options.tp) == 1 ? false : true
                }
            })
        }
        _this.GetShopList();
        setTimeout(function () {
            _this.GetList();
        }, 1000);
    },
    //获取列表数据
    GetList: function () {
        var _this = this;
        wx.showLoading({
            title: '加载中',
        });
        var param = _this.data.param;
        param.reg = _this.data.shoplist[_this.data.selectshop].RegisterNumber;
        app.http.POST('/MiniManagerAchievement/GetIncomeFlowList', param, (err, data) => {
            wx.hideLoading();
            if (data.code == 1) {
                //循环数据
                var list = data.data.Statistics;
                for (var i = 0; i < list.length; i++) {
                    list[i].BillDate = app.common.dateFormatType(list[i].BillDate, 1);
                }
                _this.setData({
                    incomedata: {
                        listdata: list,
                        totail: data.data.Totail
                    }
                });
            } else if (data.code == 2) {
                wx.hideLoading();
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
            else {
                wx.hideLoading();
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    //获取店铺列表
    GetShopList: function () {
        var _this = this;
        app.http.POST('/MiniManagerShop/GetShopList', {}, (err, data) => {
            if (data.code == 1) {
                if (data.data.length < 1) {
                    return;
                }
                _this.setData({
                    "shoplist": data.data,
                    "param.reg": data.data[0].RegisterNumber
                });
            } else if (data.code == 2) {
                wx.hideLoading();
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
            else {
                wx.hideLoading();
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        });
    },
    //显示详情
    ShowDetail: function (e) {
        var dataset = e.currentTarget.dataset;
        var openindex = this.data.openindex;//原打开的卡片index
        var newindex = -1;
        if (openindex != parseInt(dataset.id)) {
            newindex = parseInt(dataset.id);
        }
        this.setData({
            openindex: newindex
        })
    },
    //日期类型筛选
    DateChange: function (e) {
        var dataset = e.currentTarget.dataset;
        var id = parseInt(dataset.id);
        var iscustom = this.data.iscustom;
        if (id == 4) {//自定义选择时间
            iscustom = true;
            this.setData({
                "iscustom": iscustom,
                "dateindex": id
            });
            if (this.data.startdate != "") {
                this.setData({
                    "param.startDate": this.data.startdate
                });
            }
            if (this.data.enddate != "") {
                this.setData({
                    "param.endDate": this.data.enddate
                });
            }
        }
        else {
            iscustom = false;
            var time = app.common.getTimeSection(id, "yyyy-MM-dd");
            this.setData({
                "iscustom": iscustom,
                "dateindex": id,
                "param.startDate": time.sdate,
                "param.endDate": time.edate
            })
            this.GetList();
        }
    },
    //开始时间筛选
    startDateChange: function (e) {
        this.setData({
            "param.startDate": e.detail.value,
            "startdate": e.detail.value
        });
    },
    //结束时间筛选
    endDateChange: function (e) {
        this.setData({
            "param.endDate": e.detail.value,
            "enddate": e.detail.value
        });
    },
    //自定义筛选
    CustomScreen: function () {
        var _this = this;
        var startdate = _this.data.startdate;
        var enddate = _this.data.enddate;
        if (startdate == "" && enddate == "") {
            wx.showToast({
                title: '请至少选择一个时间',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        _this.GetList();
    },
    //店铺筛选
    bindPickerChange: function (e) {
        this.setData({
            selectshop: e.detail.value
        })
        this.GetList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      wx.setNavigationBarTitle({
        title: '收入统计'
      })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})